package com.xfmax.textcomponent.taskContain;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xfmax.textcomponent.R;
import com.xfmax.textcomponent.animator.AnimTools;
import com.xfmax.textcomponent.item.ChildItem;
import com.xfmax.textcomponent.item.Item;

import java.util.List;

/**
 * Created by base on 18/1/24.
 */

public class DailyTaskLayout extends BaseLayout {
    private boolean on = false;
    private int l2Height;
    private static final int DURATION = 500;
    private static final int DURATION_FIRST = 5;
    private long lastOnClickTime = 0l;
    private static final long CLICK_INTERVAL = DURATION+100;
    public DailyTaskLayout(Context context) {
        super(context);
    }

    public DailyTaskLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DailyTaskLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void addParent(String text) {
        super.addParent(text);
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.item_histroy_task, null);

        TextView textView = view.findViewById(R.id.text_title);
        textView.setText(text);

        ImageView icon = view.findViewById(R.id.image_icon);
        icon.setImageResource(R.drawable.histroy_daily_icon);

        final ImageView arrow = view.findViewById(R.id.image_detail_arrow);
        arrow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (System.currentTimeMillis() - lastOnClickTime > CLICK_INTERVAL) {
                    if (on) {
                        on();
                        arrow.setImageResource(R.drawable.histroy_arrow_up);
                    } else {
                        off();
                        arrow.setImageResource(R.drawable.histroy_arrow_down);
                    }
                }
                lastOnClickTime = System.currentTimeMillis();
            }
        });

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,150);
        addView(view,lp);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        post(new Runnable() {
            @Override
            public void run() {
                l2Height = getHeight();
                // w = mLinearLayout2.getWidth();
                //AnimTools.anim(mLinearLayout2,mLinearLayout2.getHeight(),mLinearLayout2.getHeight(),1);

                //Toast.makeText(MainActivity.this, "h:" + l2Height, Toast.LENGTH_SHORT).show();
                off(DURATION_FIRST);
            }
        });
    }

    public void on() {
//        int height = DensityUtil.dip2px(getContext(), getChildAt(0).getHeight());
        on(DURATION);
    }

    private void on(int duration) {
        int height = getChildAt(0).getHeight();
        AnimTools.anim(this, height, l2Height, duration);
        on = false;
    }

    public void off() {
        off(DURATION);
    }

    private void off(int duration) {
        l2Height = getHeight();
//        int height = DensityUtil.dip2px(getContext(), getChildAt(0).getHeight());
        int height = getChildAt(0).getHeight();
        AnimTools.anim(this, getHeight(), height, duration);
        on = true;
    }

    @Override
    public void addChild(ChildItem childItem) {
        super.addChild(childItem);
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.item_histroy_task_child, null);

        TextView title = (TextView) view.findViewById(R.id.text_title);
        title.setText(childItem.getTitle());

        TextView completeStatus = (TextView) view.findViewById(R.id.text_complete_status);
        completeStatus.setText(childItem.getCompleteStatus());

        TextView activeValue = (TextView) view.findViewById(R.id.text_active_value);
        activeValue.setText(childItem.getActiveValue());

        addView(view);
    }

    public void addLine() {
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.item_histroy_task_line, null);
        addView(view);
    }

    @Override
    public void addItem(Item item) {
        super.addItem(item);
        addParent(item.getParentTitle());
        List<ChildItem> childItemList = item.getChildItem();
        int num = 0;
        for (ChildItem childItem: childItemList) {
            num++;
            addChild(childItem);
            if (num != childItemList.size()) {
                addLine();
            }
        }

    }
}
